package com.mingben.betplatform.security;

import com.mingben.betplatform.main.Baoge3Main;
import com.mingben.betplatform.util.ThreadUtil;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileReader;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.time.LocalTime;
import java.util.Date;
import java.util.Properties;

/**
 *  //产品授权 thread daemon
 */
public class AuthorizationThread extends Thread {
    private Logger logger = LoggerFactory.getLogger(AuthorizationThread.class);
    @SneakyThrows
    @Override
    public void run() {
        //设置寄生线程
//        this.setDaemon(true);

        //轮训检测文件是否存在
        if(!new File(Baoge3Main.configPath).exists()){
            logger.error(" config.properties not exist ,, {} ",new File(Baoge3Main.configPath).getAbsolutePath());
           System.exit(0);
        }
        Properties properties = new Properties();
        properties.load(new FileReader(new File(Baoge3Main.configPath)));
        if( properties.get("auth") == null || properties.get("auth").equals("")){
            logger.error("config.properties exist ,but authorized code is null  , ");
            System.exit(0);
        }

        String privateKey = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALz3a0DWZvkJwev_HCNoaWJiuAL6r0QKJJkozO8zP7z4vyKscxgCN_f5MUVvlYtHfBhSzZ9GHLOXnf_gD16LW-koPip0jU991N2H2RKAzi2pGYkJqt88VQwNrWWMinzAUboP3j-eJypQwDeawujyhBMBmkoiaJkxEciKpAibE9ILAgMBAAECgYAHbXI5yjOh9lA5Bn4EpkX-KAMzmsbo1b06h_AGNu16cAyTQFQFzZCFnrXhFNG798NY8WhDqm3WYU9Ej3oLLU8feZlKp_A08CSZm1vl7LFtFGuLolnaPw0uGE1S8wJux6IFiuqoxKk92X_zEHUOhw_-FfjHwsq5erRJUAvv09aAAQJBAPjbWiUyl-Wzo__H4qY4qsV1PmCPyWc4sysQIYAV9cziyHggkFy5dRNwSh5xU37JkkmL7D28ZELSFcjCVpoBDAECQQDCY_sekq4E5Wxq5iCwgwykxa10Fqc7ugwEdfx2dFltydui2fCEAuyehlTMmtPZo7r1Yt2JysnbY97ZNfsrYE4LAkEA3WRIDk-0UuY-QI_TU42WX5SuqblBtFL9EpKHOyxOQLiBIF3YEKXtQh3j0rF_Vox9WHDi-wlfKRM9zh0WbmwAAQJBAImvLUfbVryLD36r3w2G1ObESPYHsY3BWg-yzg3MnScBKbswKIYoWBZ0SQ3ewBzQNnTmAMo_QzhMhaRN8sgnXz8CQQDvC6AvDLEzyfF6w_s68QrETU_y6So52B0xHPL62w-sKAYf1RrgxKTBPNwPkI3NrX0COFGUR67M5G_sRT9P4rBd";
        //私钥解密
        String decodedData = null;
        try {
            decodedData = RSAUtils.privateDecrypt(properties.get("auth").toString(), RSAUtils.getPrivateKey(privateKey)); //传入密文和私钥,得到明文
        } catch (NoSuchAlgorithmException e) {
            //保证授权运行
            logger.error("authorized fail  ,no alogrithim, ");
            System.exit(0);
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            logger.error("authorized fail  ,bad key, ");
            System.exit(0);
        }catch (Exception e ){
            //任何异常都要退出程序　防止授权线程退出　而主程序不退出
            logger.error("authorized fail  ,bad key, ");
            e.printStackTrace();
            System.exit(0);
        }
        //不死线程 每十分钟检测一次是否到期.

        //第一次已经买过 但是已经到期了 还是要校验时间
        Date expiredDate = null;
        try {
            expiredDate = new Date(Long.valueOf(decodedData));
        }catch (NumberFormatException e){
            logger.error("authorized fail  ,bad key nf, ");
            System.exit(0);
        }

        while (true){
            //当前时间小于授权时间
            if(new Date().after(expiredDate)){
                logger.error("authorized fail  ,has expired , ");
                System.exit(0);
            }

            //防止浪费不必要的性能消耗
            ThreadUtil.sleep10M();
        }
    }
}
